<?php
/**
 * utility functions
 * @author Joe Minichino <joe@dsforge.net>
 * 
 */

function getUriSegments(){
    return @split("/",  substr($_SERVER['REQUEST_URI'],1));
}

function show($object){
    echo "<pre>";
    var_dump($object);
    echo "</pre>";
}

function getDb($array){
    $mysqli = new mysqli($array['host'],$array['username'],$array['password'],$array['dbname']);
    return $mysqli;
}

function base_url(){
    if(!defined('BASE_URL')) {
        $array = parse_ini_file("config.ini",true);
        $param = $array['GENERAL'];
        return $param['baseurl'];
    }
}

function includeJquery(){

    $js = "<script type='text/javascript' src='". BASE_URL ."js/jquery.js'></script>";
    $js .= "\n<script type='text/javascript' src='" . BASE_URL ."js/jquery-ui.js'></script>\n";
    echo $js;
}

function addCssFile($file){
    echo "<link href='" .BASE_URL ."css/$file.css' rel='stylesheet' />\n";
}

function addJsFile($file){
    echo "<script type='text/javascript' src='" .BASE_URL ."js/$file.js' ></script>\n";
}

function isAjax(){
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        return true;
    } else {
        return false;
    }
}

function modelAttribute($class, $type="post"){
    //show($_POST);
    $var = new $class;
    foreach ($var->fields as $field) {
        if(count($_POST) > 0) {
            $var->$field = $_POST[$field];
        } else {
            $var->$field = $_GET[$field];
        }
    }
    return $var;
    
}

function wrapFields($array){
    $string = "";
    foreach($array as $field){
        
        $string .= ",`$field`";
    }
    $string = substr($string, 1);
    return $string;
}
